﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace FCRE
{
    /// <summary>
    /// 跨线程操作控件
    /// https://github.com/SsssGray/commonHelper/blob/master/CommonHelper/ControlHelper.cs
    /// </summary>
    public class ThreadControl
    {
        #region 改变控件的文字/text
        /// <summary>
        /// 改变控件的文字/text
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="text">文字</param>
        public static void ChangeText(Control control, string text)
        {
            if (control.InvokeRequired)
            {
                Action<string> action = x => { control.Text = x.Trim(); };
                //control.BeginInvoke(action, text);
                control.Invoke(action, text);
            }
            else
            {
                control.Text = text.Trim();
            }
        }
        #endregion

        #region 改变控件的背景色
        /// <summary>
        /// 改变控件的背景色
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="text">文字</param>
        public static void ChangeBackColor(Control control, Color color)
        {
            if (control.InvokeRequired)
            {
                Action<Color> action = x => { control.BackColor = color; };
                //control.BeginInvoke(action, text);
                control.Invoke(action, color);
            }
            else
            {
                control.BackColor = color;
            }
        }
        #endregion

        #region 增加文字到textbox
        /// <summary>
        /// 增加文字到textbox
        /// </summary>
        /// <param name="control"></param>
        /// <param name="text">增加的文字</param>
        /// <param name="time">是否在前面加上时间</param>
        public static void AppendText(TextBoxBase control, string text, bool time = false)
        {
            if (control.InvokeRequired)
            {
                Action<string> action = x =>
                {
                    if (time)
                    {
                        control.AppendText(DateTime.Now.ToString() + "  " + x + "\r\n");
                    }
                    else
                    {
                        control.AppendText(x + "\r\n");
                    }
                    control.SelectionStart = control.Text.Length;
                    control.SelectionLength = 0;
                    control.Focus();
                };
                control.Invoke(action, text);
            }
            else
            {
                if (time)
                {
                    control.AppendText(DateTime.Now.ToString() + "  " + text + "\r\n");
                }
                else
                {
                    control.AppendText(text + "\r\n");
                }
                control.SelectionStart = control.Text.Length;
                control.SelectionLength = 0;
                control.Focus();
            }
        }
        #endregion

        #region 控件是否显示
        /// <summary>
        /// 控件是否显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="statu"></param>
        public static void ChangeVisable(Control control, bool statu)
        {
            if (control.InvokeRequired)
            {
                Action<bool> action = x => { control.Visible = x; };
                control.Invoke(action, statu);
            }
            else
            {
                control.Visible = statu;
            }
        }
        #endregion

        #region 改变控件Enable状态
        /// <summary>
        /// 改变控件Enable状态
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="statu">true/false</param>
        public static void ChangeEnable(Control control, bool statu)
        {
            if (control.InvokeRequired)
            {
                Action<bool> action = x => { control.Enabled = x; };
                control.Invoke(action, statu);
            }
            else
            {
                control.Enabled = statu;
            }
        }
        #endregion

        #region 读取控件的值
        private delegate string delegateRead(Control control);
        private static string readControlText(Control control)
        {
            return control.Text.Trim();
        }

        /// <summary>
        /// 读取控件的text值
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public static string ReadText(Control control)
        {
            if (control.InvokeRequired)
            {
                delegateRead reads = new delegateRead(readControlText);
                object text = control.Invoke(reads, control);
                return (string)text;
            }
            else
            {
                return control.Text;
            }
        }
        #endregion
    }
}
